package com.editso.haier.storage;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.List;

/**
 * 存储服务接口
 */
public interface Storage {

    /**
     * 存储名称
     */
    default String getName() {
        return this.getClass().getSimpleName();
    }

    /**
     * 根uri
     */
    URI getUri();

    String getScheme();

    default URI getRelPath(String path) {
        String uri = getUri().toString();
        if (uri.endsWith(File.separator) && path.startsWith(File.separator)) {
            uri = uri.substring(uri.length() - 1).concat(path);
        } else if (!uri.endsWith(File.separator) && path.startsWith(File.separator)) {
            uri = uri.concat(path);
        } else if (!uri.endsWith(File.separator) && !path.startsWith(File.separator)) {
            uri = uri.concat(File.separator).concat(path);
        }
        return URI.create(uri);
    }

    /**
     * 保存一个文件
     *
     * @return 返回存储的文件名
     */
    String save(String path, String fileName, InputStream stream, String contentType) throws IOException;

    FileItem get(String path, String fileName) throws IOException;

    boolean delete(String path, String fileName);

    boolean update(String path, String fileName, InputStream stream);

    boolean exists(String path, String fileName);

    List<FileItem> fileList(String path) throws IOException;

    interface FileItem {
        boolean isDirectory();

        boolean isFile();

        String getName();

        String getPath();

        String getContentType();

        long getLength();

        InputStream stream() throws IOException;
    }
}
